package org.itboys.framework.spring.controller;

import java.math.BigDecimal;
import java.util.Date;

import org.itboys.commons.utils.tools.LongUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;

import org.bson.types.ObjectId;

import org.itboys.framework.spring.editor.EditorHolder;

import javax.servlet.http.HttpServletRequest;

/**
 * Controller 父类
 * @author ChenJunhui
 */
public abstract class BaseController {
	
	protected  Logger logger = LoggerFactory.getLogger(getClass());
	protected  String TAG = getClass().getSimpleName();

	@InitBinder
    public void initBinder(WebDataBinder binder) {
        binder.registerCustomEditor(Date.class, EditorHolder.dateEditor);
        binder.registerCustomEditor(Integer.class, EditorHolder.integerEditor);
        binder.registerCustomEditor(Long.class, EditorHolder.longEditor);
        binder.registerCustomEditor(BigDecimal.class, EditorHolder.bigDecimalEditor);
        binder.registerCustomEditor(ObjectId.class , EditorHolder.objectIdEditor);
    }


    public Model packagePageParam(Model model, Long totalLong, HttpServletRequest request){
        Integer total = LongUtil.isNotZreo(totalLong)?Integer.valueOf(totalLong.toString()):0;
        int pageSize = (request.getParameter("pageSize")!=null&&request.getParameter("pageSize")!="")?Integer.valueOf(request.getParameter("pageSize")):50;
        int pageNumber = (request.getParameter("pageNumber")!=null&&request.getParameter("pageNumber")!="")?Integer.valueOf(request.getParameter("pageNumber")):0;
        String order =request.getParameter("order");
        String sort = request.getParameter("sort");
        int totalPage = total==0?0:((total/pageSize)+1);//总页数
        int minPage =1;//最小页数
        int maxPage = totalPage;//最大页数
        int currentPage = pageNumber;//当前页
        model.addAttribute("total",total);
        model.addAttribute("totalPage",totalPage);
        model.addAttribute("minPage",minPage);
        model.addAttribute("maxPage",maxPage);
        model.addAttribute("currentPage",currentPage);
        model.addAttribute("nextPage",currentPage+1);
        model.addAttribute("prevPage",currentPage-1);
        return  model;
    }

	
	
}
